Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Occasional Long Operations #84

Merged
merged 2 commits into from
Dec 27, 2024
Merged

Fix Occasional Long Operations #84

merged 2 commits into from
Dec 27, 2024

Conversation

VVoruganti
Copy link
Collaborator

Through some debugging it looks like we were not handling connection pool logic properly causing a large amount of stale connections to exist on the database.

My hypothesis is that occasionally the database would saturate causing it to take up to 15 minutes for a connection to be available to complete even simple database operations like get_or_create_app

I used some of the below queries on Postgres to check for connections.

Check for idle connections older than an hour

SELECT pid, 
       usename,
       application_name,
       state,
       state_change,
       query
FROM pg_stat_activity 
WHERE 
    state = 'idle' 
    AND state_change < current_timestamp - interval '1 hour'
    AND pid <> pg_backend_pid()
    AND usename = 'postgres';

Check for all idle connections

SELECT 
    datname, 
    usename, 
    application_name,
    state, 
    age(clock_timestamp(), state_change) as state_duration,
    age(clock_timestamp(), query_start) as query_duration,
    query
FROM pg_stat_activity 
WHERE state = 'idle'
ORDER BY state_duration DESC;

Stats on database connection capacity

SELECT current_setting('max_connections') as max_connections,
       count(*) as current_connections,
       sum(CASE WHEN state = 'active' THEN 1 ELSE 0 END) as active_connections
FROM pg_stat_activity;

Originally before the changes I would see upwards of 45 connections, now it is closer to 14. I also do not see any idle connections anymore.

@VVoruganti VVoruganti marked this pull request as ready for review December 27, 2024 04:45
Copy link

@bLopata bLopata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff. I don't imagine this would require any doc updates, but happy to help with any of that as needed.

@VVoruganti VVoruganti merged commit 079084e into main Dec 27, 2024
3 checks passed
@VVoruganti VVoruganti deleted the vineeth/dev-542 branch December 27, 2024 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants